package com.pkk.penguin.components.starter.rpc.crypto.configure;

import com.pkk.penguin.components.starter.rpc.crypto.properties.SecretProperties;
import com.pkk.penguin.components.starter.rpc.crypto.service.CryptoService;
import com.pkk.penguin.components.starter.rpc.crypto.service.RsaCryptService;
import com.pkk.penguin.components.starter.rpc.crypto.constants.CommonConstants;
import javax.annotation.Resource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author luffy
 * @description: 初始化加密解密
 * @date 2019-11-18 17:43
 */
@Configuration
@ConditionalOnProperty(prefix = CommonConstants.SECRET_PROPERTIES_PREFIX, name = "enabled", havingValue = "true")
public class InitCryptoService {

  @Resource
  private SecretProperties secretProperties;

  @Bean
  @ConditionalOnProperty(prefix = CommonConstants.SECRET_PROPERTIES_PREFIX, name = "secretType", havingValue = "RSA")
  public CryptoService rsaCryptService() {
    assert secretProperties.getRsa() != null;
    return new RsaCryptService(secretProperties.getRsa().getPrivateKey(), secretProperties.getRsa().getPublicKey());
  }

}
